package com.sqfw.project.knowledge.service.impl;

import com.alibaba.fastjson2.JSON;
import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.framework.security.LoginUser;
import com.sqfw.project.knowledge.domain.KlSearchHistoryRecord;
import com.sqfw.project.knowledge.mapper.KlSearchHistoryRecordMapper;
import com.sqfw.project.knowledge.service.IKlSearchHistoryRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 搜索历史记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-14
 */
@Service
public class KlSearchHistoryRecordServiceImpl implements IKlSearchHistoryRecordService {
    @Autowired
    private KlSearchHistoryRecordMapper klSearchHistoryRecordMapper;


    /**
     * 新增搜索历史记录
     * 
     * @param klSearchHistoryRecord 搜索历史记录
     * @return 结果
     */
    @Override
    public int insertKlSearchHistoryRecord(KlSearchHistoryRecord klSearchHistoryRecord) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        KlSearchHistoryRecord record = new KlSearchHistoryRecord();
        record.setUserId(loginUser.getUserId().toString());
        record.setSearchType(klSearchHistoryRecord.getSearchType());
        KlSearchHistoryRecord oldRecord = klSearchHistoryRecordMapper.selectKlSearchHistoryRecord(record);
        // 没有就新增
        if (oldRecord == null) {
            record.setCreateTime(new Date());
            List<String> searchContentList = new ArrayList<>();
            searchContentList.add(klSearchHistoryRecord.getSearchContentJson());
            record.setSearchContentJson(JSON.toJSONString(searchContentList));
            return klSearchHistoryRecordMapper.insertKlSearchHistoryRecord(record);
        // 有就修改
        }else {
            List<String> searchContentList = JSON.parseArray(oldRecord.getSearchContentJson(), String.class);
            searchContentList.add(klSearchHistoryRecord.getSearchContentJson());
            List<String> searchList = searchContentList.stream().distinct().collect(Collectors.toList());
            // 最多保存10条数据, 看和前端的约定
            // 把最后一条数据移到第一位
            List<String> newSearchList = rightMove(searchList, 1);
            // 判断是否超过10条，超过删除最后一条
            if (newSearchList.size() > 10) {
                newSearchList.remove(newSearchList.size() - 1);
            }
            String searchListJson = JSON.toJSONString(newSearchList);
            record.setSearchContentJson(searchListJson);
            record.setId(oldRecord.getId());
            record.setUpdateTime(new Date());
            return klSearchHistoryRecordMapper.updateKlSearchHistoryRecord(record);
        }

    }

    /**
     * 修改搜索历史记录
     * 
     * @param klSearchHistoryRecord 搜索历史记录
     * @return 结果
     */
    @Override
    public int updateKlSearchHistoryRecord(KlSearchHistoryRecord klSearchHistoryRecord)
    {
        klSearchHistoryRecord.setUpdateTime(DateUtils.getNowDate());
        return klSearchHistoryRecordMapper.updateKlSearchHistoryRecord(klSearchHistoryRecord);
    }


    /**
     * 删除搜索历史记录信息
     * 
     * @param id 搜索历史记录主键
     * @return 结果
     */
    @Override
    public int deleteKlSearchHistoryRecordById(String id)
    {
        return klSearchHistoryRecordMapper.deleteKlSearchHistoryRecordById(id);
    }


    @Override
    public KlSearchHistoryRecord queryKlSearchHistoryRecord(KlSearchHistoryRecord klSearchHistoryRecord) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        KlSearchHistoryRecord record = new KlSearchHistoryRecord();
        record.setUserId(loginUser.getUserId().toString());
        record.setSearchType(klSearchHistoryRecord.getSearchType());
        KlSearchHistoryRecord oldRecord = klSearchHistoryRecordMapper.selectKlSearchHistoryRecord(record);
        if (oldRecord == null) {
            return null;
        }
        List<String> searchContentList = JSON.parseArray(oldRecord.getSearchContentJson(), String.class);
        oldRecord.setSearchContentList(searchContentList);
        return oldRecord;
    }



    /**
     * 数组向右移动
     * @param list 原数组
     * @param b 移动的位数
     * @return 移动后b位后得到的数组
     */
    private  List<String> rightMove(List<String> list, int b) {
        int originSize = list.size();
        b = originSize - b % originSize;
        for (int i = 0; i < b; i++) {
            list.add(list.get(i));
        }
        return list.subList(list.size() - originSize, list.size());
    }


}
